package com.fjec.market.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.util.DateUtil;
import com.fjec.common.util.SequenceIdGenerator;
import com.fjec.market.entity.InfoPersonal;
import com.fjec.market.mapper.InfoPersonalMapper;
import com.fjec.market.service.IInfoPersonalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.elasticsearch.common.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 049.个人用户表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-09-24
 */
@Service
public class InfoPersonalServiceImpl extends ServiceImpl<InfoPersonalMapper, InfoPersonal> implements IInfoPersonalService {

    @Resource
    private InfoPersonalMapper mapper;

    @Resource
    private SequenceIdGenerator idGenerator;

    @Override
    public boolean save(Long userId, Long coopId, String nickName, String headPic) {
        InfoPersonal entity = new InfoPersonal();
        entity.setId(idGenerator.nextId());
        entity.setUserId(userId);
        if (coopId != null && coopId != 0) {
            entity.setCoopId(coopId);
        }
        if (!Strings.isNullOrEmpty(nickName)) {
            entity.setNickName(nickName);
        }
        if (!Strings.isNullOrEmpty(headPic)) {
            entity.setHeadPic(headPic);
        }
        entity.setCreateTime(DateUtil.nowLdt());
        entity.setBusiType(1);
        return this.save(entity);
    }

    @Override
    public boolean updateBusiTypeByUserId(Long userId, Integer busiType) {
        UpdateWrapper<InfoPersonal> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("UserId", userId);
        InfoPersonal entity = new InfoPersonal();
        entity.setBusiType(busiType);
        entity.setUpdateTime(DateUtil.nowLdt());
        boolean ret = this.update(entity, updateWrapper);
        return ret;
    }

    @Override
    public boolean updateNickNameAndHeadPicByUserId(Long userId, String nickName, String headPic) {
        boolean ret = false;
        if (!StringUtils.isEmpty(nickName) || !StringUtils.isEmpty(headPic)) {
            UpdateWrapper<InfoPersonal> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("UserId", userId);
            InfoPersonal entity = new InfoPersonal();
            if (!StringUtils.isEmpty(nickName)) {
                entity.setNickName(nickName);
            }
            if (!StringUtils.isEmpty(headPic)) {
                entity.setHeadPic(headPic);
            }
            entity.setUpdateTime(DateUtil.nowLdt());
            ret = this.update(entity, updateWrapper);
        }
        return ret;
    }
}
